package cn.javaxc.mvcwork.util.helper;
import java.lang.reflect.Method;
import java.util.*;
import org.springframework.web.context.*;
import cn.javaxc.mvcwork.util.common.SpringUtils;
public class ObjectHelper { 

  public static Object invokeObject(Object cls,String mtd_name,Object[] args,Class<?>... types) {
		
		Method mtd=null;
		Object ret=null;
		try{
		ret=invokeObjectEx(cls,mtd_name,args,types);
		}catch(Exception e){
			e.printStackTrace();
		}
		return ret;
  }
  public static Object invokeObjectEx(Object cls,String mtd_name) throws Exception{
		return invokeObjectEx(cls,mtd_name,new Object[0],new Class[0]);
  }
  public static Object invokeObjectEx(String cls,String mtd_name) throws Exception{
		return invokeObjectEx(cls,mtd_name,new Object[0],new Class[0]);
  }
  public static Object invokeObjectEx(String cls,String mtd_name,Object[] args,Class<?>... types) throws Exception{
		return invokeObjectEx(Class.forName(cls).newInstance(),mtd_name,args,types);
  }
  public static Object invokeObjectEx(Object cls,String mtd_name,Object[] args,Class<?>... types) throws Exception{
		if(cls==null)return null;
		Method mtd=null;
		Object ret=null;
//		if(System.getProperty("DEBUG")!=null)System.out.println(GenericUtil.class.getName()+".invokeObjectEx "+mtd_name+" "+Arrays.toString(args));
		mtd=cls.getClass().getMethod(mtd_name,types);
		mtd.setAccessible(true); 
		if(args.length==0){
			ret=mtd.invoke(cls);
		}else{
			ret=mtd.invoke(cls,args);
		}

		return ret;
  }
  public static boolean hasMethod(Object obj,String mtd_name){
		for (Method method:obj.getClass().getDeclaredMethods() ){
			if (method.getName().equals(mtd_name)){
				return true;
			}
		}
		for (Method method:obj.getClass().getMethods() ){
			if (method.getName().equals(mtd_name)){
				return true;
			}
		}
		return false;
	}
	public static <T>T cast(Object o,Class<T> e){
		return e.cast(o);
	}

  public static Object getBean(String bean){
		return SpringUtils.getBean(bean);
	}
	public static <T> T getBean(Class<T> clazz){
		return SpringUtils.getBean(clazz);
	}

}
